home *** CD-ROM | disk | FTP | other *** search
/ Games of Daze / Infomagic - Games of Daze (Summer 1995) (Disc 1 of 2).iso / djgpp / contrib / bcc2grx / src / bccgrx.c next >
Encoding:
C/C++ Source or Header  |  1994-05-21  |  8.3 KB  |  227 lines

  1. /*
  2.  *  BCC2GRX  -  Interfacing Borland based graphics programs to LIBGRX
  3.  *  Copyright (C) 1993  Hartmut Schirmer
  4.  *
  5.  *  This library is free software; you can redistribute it and/or
  6.  *  modify it under the terms of the GNU Library General Public
  7.  *  License as published by the Free Software Foundation; either
  8.  *  version 2 of the License, or (at your option) any later version.
  9.  *
  10.  *  This library is distributed in the hope that it will be useful,
  11.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  13.  *  Library General Public License for more details.
  14.  *
  15.  *  You should have received a copy of the GNU Library General Public
  16.  *  License along with this library; if not, write to the Free
  17.  *  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18.  *
  19.  *
  20.  *  Contact :                Hartmut Schirmer
  21.  *                           Feldstrasse 118
  22.  *                    D-2300 Kiel 1
  23.  *                           Germany
  24.  *
  25.  *                    No telephone calls please !
  26.  *
  27.  *           NOTE : Please use ZIP 24105 after July 1 1993 !!
  28.  *           
  29.  *
  30.  *  Changes :  940505 (Per Allansson c91peral@und.ida.liu.se)
  31.  *
  32.  *              see 'changes'
  33.  */
  34.  
  35. #define __BCCGRX_C
  36.  
  37. #include "bccgrx00.h"
  38. #include <malloc.h>
  39.  
  40. /* ----------------------------------------------------------------- */
  41.  
  42. int           __gr_Mode = 0;                    /* actual graphics mode     */
  43. int           __gr_INIT = FALSE;                /* TRUE after initgraph()   */
  44. char          __gr_BGICHR[128];                 /* Path to .chr files       */
  45. int           __gr_MaxMode = 0;                 /* Last available mode      */
  46. int           __gr_Result = grOk;               /* stores error code        */
  47. int           __gr_X, __gr_Y;                   /* graphics cursor pos      */
  48. int           __gr_vpl, __gr_vpt,               /* actual viewport          */
  49.           __gr_vpr, __gr_vpb;
  50. int           __gr_color;                       /* drawing color            */
  51. int           __gr_colorbg;                     /* background color         */
  52. int           __gr_colorfill;                   /* fill color               */
  53. int           __gr_WR = GrWRITE;                /* Write mode               */
  54. int           __gr_lstyle = SOLID_LINE;         /* Actual line style        */
  55. int           __gr_fpatno = SOLID_FILL;         /* Actual filling pattern   */
  56. int           __gr_Xasp = 10000;                /* Aspect ratio             */
  57. int           __gr_Yasp = 10000;
  58. int           __gr_clip = TRUE;                 /* actual clipping state    */
  59. int           __gr_ADAPTER = GR_VGA;            /* Adapter used             */
  60.  
  61. short         *__gr_modeindx = NULL;            /* mode # -> driver mode    */
  62. GR_DRIVER_MODE_ENTRY *__gr_DATA;                /* ptr to driver mode data  */
  63. GrPattern     __gr_fillpattern;                 /* GRX filling settings     */
  64. GrLineOption  __gr_Line;                        /* GRX line settings        */
  65.  
  66. char __gr_fpatterns[][8] = {                    /* BGI fill patterns        */
  67.   { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00},   /* EMPTY_FILL        */
  68.   { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF},   /* SOLID_FILL        */
  69.   { 0xFF, 0xFF, 0x00, 0x00, 0xFF, 0xFF, 0x00, 0x00},   /* LINE_FILL         */
  70.   { 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80},   /* LTSLASH_FILL      */
  71.   { 0xE0, 0xC1, 0x83, 0x07, 0x0E, 0x1C, 0x38, 0x70},   /* SLASH_FILL        */
  72.   { 0xF0, 0x78, 0x3C, 0x1E, 0x0F, 0x87, 0xC3, 0xE1},   /* BKSLASH_FILL      */
  73.   { 0xA5, 0xD2, 0x69, 0xB4, 0x5A, 0x2D, 0x96, 0x4B},   /* LTBKSLASH_FILL    */
  74.   { 0xFF, 0x88, 0x88, 0x88, 0xFF, 0x88, 0x88, 0x88},   /* HATCH_FILL        */
  75.   { 0x81, 0x42, 0x24, 0x18, 0x18, 0x24, 0x42, 0x81},   /* XHATCH_FILL       */
  76.   { 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33, 0xCC, 0x33},   /* INTERLEAVE_FILL   */
  77.   { 0x80, 0x00, 0x08, 0x00, 0x80, 0x00, 0x08, 0x00},   /* WIDE_DOT_FILL     */
  78.   { 0x88, 0x00, 0x22, 0x00, 0x88, 0x00, 0x22, 0x00},   /* CLOSE_DOT_FILL    */
  79.   { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}    /* USER_FILL         */
  80. };
  81.  
  82. int __gr_BGI_w = 640; /* resulution and colors needed to emulate */
  83. int __gr_BGI_h = 480; /* BGI driver modes                        */
  84. int __gr_BGI_c = 16;  /* default : Standard VGA                  */
  85.  
  86. /* ----------------------------------------------------------------- */
  87.  
  88. void __gr_set_up_modes(void)
  89. {
  90.   GR_DRIVER_MODE_ENTRY *tm, *gm;
  91.   int mode;
  92.   static int DidInit = FALSE;
  93.  
  94.   if (DidInit) return;
  95.  
  96.   MM = 1;
  97.   GrSetMode( GrCurrentMode());                       /* Init grx */
  98.   __gr_ADAPTER = GrAdapterType();
  99.   if (__gr_ADAPTER == GR_S3)
  100.     __gr_ADAPTER = GR_VGA;
  101.   GrGetDriverModes(&tm, &__gr_DATA);
  102.   if (__gr_DATA == NULL) {       /* Will happen with .GRD drivers */
  103.     __gr_modeindx = NULL;
  104.     DidInit = TRUE;
  105.     return;
  106.   }
  107.   gm = __gr_DATA;
  108.   mode = __FIRST_DRIVER_SPECIFIC_MODE-1;
  109.   while (gm->width != 0) {
  110.     if (gm->mode.grn.BIOS_mode != 0xFF)
  111.       ++mode;
  112.     gm += 1;
  113.   }
  114.   __gr_modeindx = (short *) malloc(sizeof(short) * mode);
  115.   if (__gr_modeindx == NULL) {
  116.     DidInit = TRUE;   /* Handle no mem like .GRD driver */
  117.     return;           /* should never (??) occur ...    */
  118.   }
  119.   __gr_modeindx[0] = -1;  /* GR_default_graphics               */
  120.   __gr_modeindx[1] = -2;  /* GR_biggest_noninterlaced_graphics */
  121.   __gr_modeindx[2] = -3;  /* GR_biggest_graphics               */
  122.   __gr_modeindx[3] = -4;  /* BGI emulation                     */
  123.   gm = __gr_DATA;
  124.   mode = __FIRST_DRIVER_SPECIFIC_MODE-1;
  125.   while (gm->width != 0) {
  126.     if (gm->mode.grn.BIOS_mode != 0xFF)
  127.       __gr_modeindx[++mode] = gm-__gr_DATA;
  128.     gm += 1;
  129.   }
  130.   MM = mode;
  131.   DidInit = TRUE;
  132. }
  133.  
  134. /* ----------------------------------------------------------------- */
  135. void graphdefaults(void)
  136. {
  137.   ERR          = grOk;
  138.   moveto( 0, 0);
  139.   COL = FILL   = GrWhite();
  140.   COLBG        = GrBlack();
  141.   __gr_WR      = GrWRITE;
  142.  
  143.   _DO_INIT_CHECK;
  144.   GrSetContext( NULL);         /* ViewPort == Full screen */
  145.   VL = VT = 0;
  146.   VR = getmaxx();
  147.   VB = getmaxy();
  148.   __gr_clip = TRUE;
  149.  
  150.   __gr_lstyle     = SOLID_LINE;
  151.   LNE.lno_pattlen = 0;
  152.   LNE.lno_dashpat = NULL;
  153.   LNE.lno_width   = 1;
  154.  
  155.   FPATT = SOLID_FILL;
  156.   FILLP.gp_ispixmap    = 0;    /* Bitmap fill pattern */
  157.   FILLP.gp_bmp_data    = (unsigned char *)&__gr_fpatterns[FPATT];
  158.   FILLP.gp_bmp_height  = 8;
  159.   FILLP.gp_bmp_fgcolor = COL;
  160.   FILLP.gp_bmp_bgcolor = COLBG;
  161. }
  162.  
  163. /* ----------------------------------------------------------------- */
  164. void detectgraph(int *graphdriver,int *graphmode)
  165. {
  166.   __gr_set_up_modes();
  167.   *graphdriver = __gr_ADAPTER;  /* set by _gr_set_up_modes () */
  168.   *graphmode     = 0;           /* Default graphics mode      */
  169. }
  170.  
  171. /* ----------------------------------------------------------------- */
  172. void setgraphmode(int mode)
  173. {
  174.   GR_DRIVER_MODE_ENTRY *gm;
  175.  
  176.   _DO_INIT_CHECK;
  177.   switch (mode) {
  178.     case GRX_DEFAULT_GRAPHICS:
  179.       GrSetMode( GR_default_graphics);
  180.       break;
  181.     case GRX_BIGGEST_NONINTERLACED_GRAPHICS:
  182.       GrSetMode( GR_biggest_noninterlaced_graphics);
  183.       break;
  184.     case GRX_BIGGEST_GRAPHICS:
  185.       GrSetMode( GR_biggest_graphics);
  186.       break;
  187.     case GRX_BGI_EMULATION:
  188.       GrSetMode( GR_width_height_color_graphics,
  189.           __gr_BGI_w, __gr_BGI_h, __gr_BGI_c);
  190.       break;
  191.     default:
  192.       if (mode < __FIRST_DRIVER_SPECIFIC_MODE || mode > MM) {
  193.     ERR = grInvalidMode;
  194.     return;
  195.       }
  196.       gm = __gr_DATA+__gr_modeindx[mode];
  197.       GrSetMode( GR_width_height_color_graphics,
  198.          gm->width, gm->height, gm->number_of_colors);
  199.       break;
  200.   }
  201.   __gr_Mode = mode;
  202.   graphdefaults();
  203.   GrClearScreen(BLACK);
  204. }
  205.  
  206. /* ----------------------------------------------------------------- */
  207. void initgraph(int *graphdriver, int *graphmode, char *pathtodriver)
  208. {
  209.   ERR = grOk;
  210.   if (!__gr_INIT) {
  211.     __gr_set_up_modes();
  212.     if (ERR != grOk) return;
  213.     if ( *graphdriver != NATIVE_GRX || *graphmode < 0 || *graphmode > MM)
  214.     *graphmode = 0;
  215.     __gr_INIT = TRUE;
  216.     setgraphmode(*graphmode);
  217.     if (ERR != grOk) {
  218.       __gr_INIT = FALSE;
  219.       return;
  220.     }
  221.   }
  222.   if (*graphmode == 0) *graphdriver = __gr_ADAPTER;
  223.           else *graphdriver = NATIVE_GRX;
  224.   strcpy( __gr_BGICHR, pathtodriver);
  225. }
  226. 
  227.